package org.jeecg.modules.iot.middleware.util;

import org.jeecg.jt808.modbus.MqttMessageJson;
import org.jeecg.jt808.req.LocationNewMsg;
import org.jeecg.modules.iot.model.Jt808Control.JT808AttributeConverter;
import org.jeecg.modules.iot.model.Location;
import org.yzh.protocol.t808.T0200;

import java.sql.Timestamp;
import java.text.ParseException;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Map;

public class LocationUtil {
    private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("yyMMddHHmmss")
            .withZone(ZoneId.of("GMT+8")); // 设置时区
    private Location createLocation(MqttMessageJson mqttMessageJson, T0200 msg) throws ParseException {
        long timestamp;
        try {
            timestamp = Instant.from(DATE_FORMAT.parse(mqttMessageJson.getTimestamp())).toEpochMilli();
        } catch (DateTimeParseException e) {
            timestamp = Instant.now().toEpochMilli(); // 使用 Instant.now() 获取当前时间戳
        }
        Map<Integer, Object> attributeConverter = msg.getAttributes();
        Integer snnNumber = (Integer) JT808AttributeConverter.convertAttributes(attributeConverter).get("gnssSatelliteCount"); //  链式调用
        Location location = new Location();
        location.setTs(new Timestamp(timestamp)); // 设置时间戳
        location.setDeviceId(mqttMessageJson.getId());
        location.setAlarm(msg.getWarnBit());
        location.setStatusField(msg.getStatusBit());
        location.setLatitude(msg.getLatitude() * 1.0 / 1000000);
        location.setLongitude(msg.getLongitude() * 1.0 / 1000000);
        location.setSnnNumber(snnNumber);
        location.setSpeed((short) msg.getSpeed());
        location.setDirection((short) msg.getDirection());
        location.setElevation((short) msg.getAltitude());
        location.setSeq(mqttMessageJson.getSeq());
        return location;
    }

    public static Location createLocationFromLocationNewMsg(MqttMessageJson mqttMessageJson, LocationNewMsg msg) {
        if (msg == null || mqttMessageJson == null) {
            return null; // 或抛出异常
        }

        long timestamp;
        try {
            timestamp = Instant.from(DATE_FORMAT.parse(mqttMessageJson.getTimestamp())).toEpochMilli();
        } catch (DateTimeParseException e) {
            // 处理解析异常，例如记录错误日志或使用默认值
            timestamp = Instant.now().toEpochMilli();
        }


        Location location = new Location();
        location.setTs(new Timestamp(timestamp));
        location.setDeviceId(mqttMessageJson.getId());
        location.setAlarm(msg.getAlarm());
        location.setStatusField(msg.getStatusField());
        location.setLatitude((double) msg.getLatitude());
        location.setLongitude((double) msg.getLongitude());

        // 其他字段，根据需要从 LocationNewMsg 中设置
        location.setElevation(msg.getElevation());
        location.setSpeed(msg.getSpeed());
        location.setDirection(msg.getDirection());

        // 设置附加信息中的字段，需要进行空值检查
        if (msg.getSpeedNew() != null) {
            location.setSpeed(msg.getSpeedNew().shortValue()); //  如果 speedNew 存在，则使用 speedNew 的值
        }

        location.setSMileage(Float.valueOf(msg.getSingleMileage() != null ? msg.getSingleMileage().longValue() : 0)); // Integer to Long
        location.setMileage(Float.valueOf(msg.getTotalMileage() != null ? msg.getTotalMileage().longValue() : 0)); // Integer to Long
        location.setSeq(mqttMessageJson.getSeq());
        return location;
    }

}
